package com.habdqn.attendance.service.impl;

import com.habdqn.attendance.dao.impl.DepartmentDaoImpl;
import com.habdqn.attendance.entity.Department;
import com.habdqn.attendance.dao.DepartmentDao;
import com.habdqn.attendance.service.DepartmentService;
import lombok.extern.log4j.Log4j;

import java.util.List;

/**
 * 部门表(Department)表服务实现类
 *
 * @author makejava
 * @since 2019-10-23 14:35:14
 */
@Log4j
public class DepartmentServiceImpl implements DepartmentService {
    private DepartmentDao departmentDao = new DepartmentDaoImpl();

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Department queryById(Integer id) {
        return this.departmentDao.queryById(id);
    }

    /**
     * 查询分页数据
     *
     * @param likeName 查询名称
     * @param pageNo 页码
     * @param pageSize  每页
     * @return 对象列表
     */
    @Override
    public List<Department> queryAllByPage(String likeName, int pageNo, int pageSize) {
        int offset = (pageNo - 1) * pageSize;
        if (likeName != null && !"".equals(likeName)) {
            return departmentDao.queryAllByLimit(likeName, offset, pageSize);
        }
        return this.departmentDao.queryAllByLimit(offset, pageSize);
    }

    @Override
    public List<Department> queryAll() {
        return this.departmentDao.queryAll(null);
    }

    /**
     * 新增数据
     *
     * @param department 实例对象
     * @return 实例对象
     */
    @Override
    public Department insert(Department department) {
        department.setId(this.departmentDao.insert(department));
        log.debug("新增" + department + "成功！");
        return department;
    }

    /**
     * 修改数据，有id则更新，没有id则新增
     *
     * @param department 实例对象
     * @return 实例对象
     */
    @Override
    public Department update(Department department) {
        // ID为空则执行新增
        if (department.getId() == null) {
            return this.insert(department);
        }
        this.departmentDao.update(department);
        log.debug("修改" + department + "成功！");
        return this.queryById(department.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.departmentDao.deleteById(id) > 0;
    }

    @Override
    public boolean batchDeleteByIds(String ids) {
        return this.departmentDao.batchDeleteByIds(ids) > 0;
    }

    @Override
    public int getAllCount(String likeName) {
        if (likeName != null && !"".equals(likeName)) {
            return departmentDao.getAllCount(likeName);
        }
        return departmentDao.getAllCount();
    }
}